package com.example.bingobox.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.example.bingobox.R;
import com.example.bingobox.model.State;

public class LocationMarkDialog extends DialogFragment {
    private static final String[] PREDEFINED_TYPES = {"家", "公司", "学校"};
    private State state;
    private OnLocationMarkListener listener;

    public static LocationMarkDialog newInstance(State state) {
        LocationMarkDialog dialog = new LocationMarkDialog();
        dialog.state = state;
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_location_mark, null);

        Spinner typeSpinner = view.findViewById(R.id.type_spinner);
        EditText customType = view.findViewById(R.id.custom_type);

        // 设置预定义类型
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
            requireContext(), 
            android.R.layout.simple_spinner_item, 
            PREDEFINED_TYPES
        );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        typeSpinner.setAdapter(adapter);

        return builder.setView(view)
                .setTitle("标记地点")
                .setPositiveButton("确定", (dialog, which) -> {
                    String type;
                    if (typeSpinner.getSelectedItemPosition() == adapter.getCount() - 1) {
                        // 使用自定义类型
                        type = customType.getText().toString().trim();
                        if (type.isEmpty()) {
                            type = "未标记";
                        }
                    } else {
                        type = PREDEFINED_TYPES[typeSpinner.getSelectedItemPosition()];
                    }
                    if (listener != null) {
                        listener.onLocationMarked(type);
                    }
                })
                .setNegativeButton("取消", null)
                .create();
    }

    public interface OnLocationMarkListener {
        void onLocationMarked(String type);
    }

    public void setOnLocationMarkListener(OnLocationMarkListener listener) {
        this.listener = listener;
    }
} 